package com.app.modules.web.pagetag;

import java.io.IOException;
import java.io.Writer;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

@SuppressWarnings("serial")
public class PageTagText2 extends TagSupport {
	private String formId = "document.forms[0]";

	public void setFormId(String formId) {
		this.formId = "document.getElementById('" + formId + "')";
	}
	
	public int doStartTag() throws JspException {
		// 取出Form里的Page对象.
		Page page = (Page) pageContext.findAttribute("page");

		// 打印隐藏翻页字段及翻页JavaScript.
		printNavigation(page);
		printTurnPageJavaScript();

		return EVAL_BODY_INCLUDE;
	}

	// 打印导航条.
	private void printNavigation(Page page) {
		StringBuffer html_body = new StringBuffer();
		html_body.append("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">").append("<tr><td width=\"50%\">");
		
		html_body.append("共").append(page.getTotalCount()).append("条纪录，当前第")
				.append(page.getPageNo()).append("/").append(page.getTotalPages()).append("页，每页")
				.append(page.getPageSize()).append("条纪录");
		
		html_body.append("</td><td align=\"right\">");
		if (!page.isHasPre()) {
			html_body.append("上一页");
		} else {
			html_body.append(getHref(page.getPrePage(),"上一页"));
		}
		
		html_body.append("&nbsp;");
		
		if (!page.isHasNext()) {
			html_body.append("下一页");
		} else {
			html_body.append(getHref(page.getNextPage(),"下一页"));
		}
		
		html_body.append("转到第<input type=\"text\" size=\"2\" id=\"pageNo\" name=\"page.pageNo\" />页<input type=\"button\" name=\"search\" value=\"-> 转\" onclick=\"jumpPage(-1)\"/>");
		html_body.append("</td></tr></table>");
		
		pageContextWrite(html_body.toString());
	}
	
	// 根据当前页获取链接字.
	private String getHref(int pageNo,String pageText) {
			StringBuffer buf = new StringBuffer();
			buf.append("<a href=\"#\"");
			buf.append(" onclick=\"jumpPage(").append(pageNo).append(");return false;\">");
			buf.append(pageText);
			buf.append("</a>");
			return buf.toString();
	}	

	// 打印隐藏翻页字段及翻页JavaScript.
	private void printTurnPageJavaScript() {
		StringBuffer buffer = new StringBuffer();
		buffer.append("<script type='text/javascript'>\n")
			  .append("function jumpPage(pageNo) {\n")
			  .append("var form = ").append(formId).append(";\n")
			  .append("if(pageNo == -1){\n")
			  .append("pageNo = form[\"pageNo\"].value;\n")
			  .append("var reg = /^([1-9][0-9]*)$/;\n")
			  .append("if(!reg.test(pageNo)){\n")
			  .append("alert(\"请输入正整数的页码 ！\");\n")
			  .append("return false;\n")
			  .append("}\n}\n")
			  .append("form[\"pageNo\"].value = pageNo;\n")
			  .append("form.submit();\n")
			  .append("}\n")
			  .append("</script>\n");
		
		pageContextWrite(buffer.toString());
	}
	
	// 打印.
	private void pageContextWrite(String html){
		try {
			Writer out = pageContext.getOut();
			out.write(html);
			out.flush();
		}
		catch (IOException e) {
			throw new RuntimeException("pageContext write.io exception");
		}
	}

}
